package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.util.DensityUtil;
import cn.kooki.app.duobao.util.ScreenUtils;

/**
 * Created by xiong on 2015/12/19.
 */
public class ListImageAdapter extends BaseAdapter {

    private ArrayList<String> urls;
    private Context context;

    public ListImageAdapter(Context context, ArrayList<String> urls) {
        this.context = context;
        this.urls = urls;
    }

    @Override
    public int getCount() {
        return urls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_share_detail_image, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ViewHolder finalViewHolder = viewHolder;
        Glide.with(context).load(urls.get(position)).asBitmap().centerCrop().error(R.drawable.img_blank).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                int width = resource.getWidth();
                int height = resource.getHeight();
                float radio = width * 1.0f / height;
                ViewGroup.LayoutParams layoutParams = finalViewHolder.shareDetailImage.getLayoutParams();
                int lwidth = ScreenUtils.getScreenWidth(context) - ScreenUtils.dpToPxInt(context, 20);
                if (height > width || height >= 200) {
                    layoutParams.height = (int) (lwidth / radio);
                } else if (height < 200) {
                    layoutParams.height = 200;
                }
                finalViewHolder.shareDetailImage.setLayoutParams(layoutParams);
                finalViewHolder.shareDetailImage.setImageBitmap(resource);
            }
        });
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_share_detail_image.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.share_detail_image)
        ImageView shareDetailImage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
